package com.example.jjc.rxjava_retrofit2_demo.adapter;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jjc.rxjava_retrofit2_demo.R;
import com.example.jjc.rxjava_retrofit2_demo.model.Item;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class ZhuangbiListAdapter extends RecyclerView.Adapter {
    List<Item> imageList;
    OnItemClickListener listener = null;
    boolean isHorizontal = false;//是否水平recyclerview

    public interface OnItemClickListener{
        void onItemClick(View view);
    }

    /**
     * 添加单击监听
     */
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    //子视图
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    listener.onItemClick(view);
                }
            }
        });
        return new DebounceViewHolder(view);
    }

    //数据对象
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DebounceViewHolder viewHolder = (DebounceViewHolder) holder;
        Item image = imageList.get(position);
        Glide.with(holder.itemView.getContext())
                .load(image.image_url)
                .placeholder(android.R.drawable.stat_sys_download)
                .into(viewHolder.ivPic); //设置图片url
        viewHolder.tvDs.setText(image.description);//填充描述
        viewHolder.itemView.setTag(position);
    }

    /**
     * 设置数据源
     */
    public void setImageList(List<Item> list){
        imageList = list;
        notifyDataSetChanged();
    }

    public List<Item> getImageList(){
        return imageList;
    }

    public void setHorizontal(boolean isHorizontal){
        this.isHorizontal = isHorizontal;
    }

    //条目总数量
    @Override
    public int getItemCount() {
        return imageList == null ? 0 : imageList.size();
    }

    class DebounceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cv)
        CardView cv;//卡片view
        @BindView(R.id.iv_pic)
        ImageView ivPic;//图片
        @BindView(R.id.tv_ds)
        TextView tvDs;//图片描述

        public DebounceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            if (isHorizontal){//如果是水平list
                cv.setRadius(0);
                cv.setLayoutParams(new CardView.LayoutParams(220,220));
                ivPic.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
                tvDs.setVisibility(View.GONE);
            }
        }
    }
}
